package rs.dalao.syc.doctor.all.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import rs.dalao.syc.common.pojo.authentication.CurrentPrincipal;
import rs.dalao.syc.common.response.JsonResult;
import rs.dalao.syc.doctor.all.pojo.param.ArticleParam;
import rs.dalao.syc.doctor.all.pojo.param.ArticleUpdateParam;
import rs.dalao.syc.doctor.all.pojo.vo.ArticleManagementVO;
import rs.dalao.syc.doctor.all.service.IArticleService;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/article")
@Api(tags = "9. 文章管理")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @PostMapping("/post")
    @PreAuthorize("hasAuthority('/article/post')")
    @ApiOperation("发表文章")
    public JsonResult postArticle(ArticleParam articleParam, @ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        articleService.insertArticle(articleParam,currentPrincipal);
        log.debug("Article" +articleParam);
        return JsonResult.ok();
    }
    @GetMapping("/getArticleList")
    @PreAuthorize("hasAuthority('/article/post')")
    @ApiOperation("获取当前医生所有文章")
    public JsonResult getArticleList(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        log.debug("ArticleList" +currentPrincipal);
        List<ArticleManagementVO> allArticleByAuthorId = articleService.getAllArticleByAuthorId(currentPrincipal.getId());
        return JsonResult.ok(allArticleByAuthorId);
    }
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/article/post')")
    @ApiOperation("根据文章ID删除文章")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除文章】的请求，参数：{}", id);
        articleService.deleteByArticleId(id);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/article/post')")
    @ApiOperation("修改文章")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id,
                                     @Valid ArticleUpdateParam articleUpdateParam) {
        log.debug("开始处理【修改文章】的请求，ID：{}，新数据：{}", id, articleUpdateParam);
        articleService.updateInfoById(id, articleUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("{id}/updateViewCounts")
    @ApiOperation("更改文章浏览量")
    @ApiOperationSupport(order = 301)
    public JsonResult selectDetail(@PathVariable Long id) {
        //让浏览量+1
        articleService.updateViewCountById(id);
        return JsonResult.ok(articleService.getArticleById(id));
    }
}
